package net.borderwars.webstartclient;

import net.borderwars.beans.Player;
import net.borderwars.beans.Robot;
import net.borderwars.jedit.JEditTextArea;
import net.borderwars.jedit.JavaTokenMarker;
import net.borderwars.jedit.TextAreaDefaults;
import net.borderwars.userserver.ServerInterface;
import org.apache.bcel.generic.Instruction;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.TreeMap;



/**
 * @author hubbard
 *         Date: May 12, 2006
 *         Time: 12:37:39 PM
 */
public class Battle {
    Player player;
    private LinkedList<Robot> bots;

    private ServerInterface server;
    private JPanel centerPanel;
    private JPanel battlePane;
    private JList lBots;
    private JPanel statusPane;
    private JPanel mainPanel;
    private JList lInstructions;
    private JButton bPause;
    private JButton bPlay;
    private JButton bStep;
    private JTable table1;
    private JPanel varPanel;
    private JComponent code;
    JEditTextArea ta;

    private JPanel codePane;
    private Robot abots[];
    private Robot selectedBot;
    private BattleThread battleThread;
    private volatile boolean paused;

    public JFrame getFrame () {
        return frame;
    }

    private JFrame frame;
    private int WIDTH = 680;
    private int HEIGHT = 480;

    public Battle (ServerInterface server, Player player, LinkedList<Robot> bots) {
        this.server = server;
        this.player = player;
        this.bots = bots;
        frame = new JFrame ("Borderwars Client");
        frame.setContentPane (centerPanel);
        centerFrame (frame, WIDTH, HEIGHT);
        frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
        abots = bots.toArray (new Robot[0]);
        lBots.setListData (abots);
        frame.show ();
        battleThread = new BattleThread (this, server, player, bots);
        lBots.setSelectedIndex (0);
        selectedBot = abots[lBots.getSelectedIndex ()];

        TextAreaDefaults tad = TextAreaDefaults.getDefaults ();
        tad.cols = 30;
        tad.rows = 5;
        ta = new JEditTextArea (tad);

        ta.setTokenMarker (new JavaTokenMarker ());

        ta.setText (selectedBot.getSource ());

        ta.setEditable (false);

        bPause.addActionListener (new ActionListener() {
            public void actionPerformed (ActionEvent e) {
                paused = !paused;
                if (paused) {
                    bStep.setEnabled (true);
                }
                battleThread.setPause (paused);
            }
        });

        lBots.addListSelectionListener (new ListSelectionListener() {
            public void valueChanged (ListSelectionEvent e) {
                selectedBot = abots[lBots.getSelectedIndex ()];
                ta.setText (selectedBot.getSource ());
            }
        });
        codePane.remove (0);
        codePane.setLayout (new BorderLayout ());
        codePane.add (ta, BorderLayout.CENTER);
        battleThread.setSleep (10);

    }

    public void updateData () {
        int lineNumber = battleThread.getSourceLineNumber (selectedBot);
        if (lineNumber > 0)
            ta.scrollTo (lineNumber, 0);

        TreeMap<Integer, Instruction> i = battleThread.getCurrentInstructions (selectedBot);
        String data[] = new String[i.size ()];
        int idx = 0;
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer> ();
        for (Map.Entry<Integer, Instruction> entry : i.entrySet ()) {
            map.put (entry.getKey (), idx);
            data[idx++] = entry.getKey () + " " + entry.getValue ();

        }
        int pc = battleThread.getCurrentIndex (selectedBot);
        lInstructions.setListData (data);
        lInstructions.setSelectedIndex (map.get (pc));
    }

    private static void centerFrame (JFrame frame, int w, int h) {
        Dimension d = Toolkit.getDefaultToolkit ().getScreenSize ();
        int sx = (d.width - w) >> 1;
        int sy = (d.height - h) >> 1;
        frame.setLocation (sx, sy);
        frame.setSize (w, h);
    }

}
